# -*- coding: utf-8 -*-
# @Author  : suny570
# @Time    : 2022-04-04 22:12


import datetime
import random
import time

#30 November 2018
def engDateToChDate(str):
    time_format = datetime.datetime.strptime(str.replace('\r','').replace('\n','').replace('\t',''), '%d %B %Y')
    return  time_format
#Posted on 30 October 2018
def engPDateToChDate(str):
    time_format = datetime.datetime.strptime(str.replace('\r','').replace('\n','').replace('\t','').replace('Posted on ',''), '%d %B %Y')
    return  time_format
#August 30, 2018
def engDouDateToChDate(str):
    try:
        time_format = datetime.datetime.strptime(str.replace('\r', '').replace('\n', '').replace('\t', '').strip(' '),'%B %d, %Y')
        return time_format
    except Exception as e:
        time_format = datetime.datetime.strptime(str.replace('\r', '').replace('\n', '').replace('\t', '').strip(' '), '%b %d, %Y')
        return time_format

# 2018-12-25
# 处理只有月份和日期不规范
def noLightDateToChDate(text):
    # 0807  > 08-07
    if len(text) == 4:
        tmp_text = list(text)
        tmp_text.insert(2,'-')
        text = ''.join(tmp_text)
    cur_year = datetime.datetime.now().year
    # '2020-03.03'
    text = text.replace('.','-').strip()

    time_format_ymd = datetime.datetime.strptime(str(cur_year) + "-" + text, "%Y-%m-%d").date()
    #datetime.datetime.strptime(str,'%Y/%m/%d')
    return time_format_ymd
# '2020-03-03'
def getLastYearMonthDate(text):
    #print(str(text[0:4]))
    last_year = int(str(text[0:4])) -1
    text = text[4:]
    #time_format_ymd = datetime.datetime.strptime(str(last_year) + text, "%Y-%m-%d").date()
    #datetime.datetime.strptime(str,'%Y/%m/%d')
    return str(last_year) + text
# '20200303 转化为 2020-03-03
def getStandarsDate(text):

    return text[0:4] + '-' + text[4:6] + '-' + text[6:]

#July 2018
def engYMDateToChDate(str):
    time_format = datetime.datetime.strptime(str.replace('\r','').replace('\n','').replace('\t',''), '%B %Y')
    return  time_format
# November 2018
def engYMSDateToChDate(str):
    time_format = datetime.datetime.strptime(str.replace('\r','').replace('\n','').replace('\t',''), ' %B %Y')
    return  time_format


def getNowTimeToStr():

    cru_time = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
    return cru_time

# 2020-04-10 变为2020年4月
def getYearAndMonth(str_time):
    text = str_time.split('-')
    #print(str_time + "\t" + str(text))
    if len(text) == 3:
        return text[0] + "年" + text[1] + "月"
    else:
        return str_time

# 2020-04-10
# 判断是否为指定日期格式
def isStandDateFormat( str_date):
    text = str_date.split('-')
    if len(text) == 3:
        if len(text[0]) == 4 and len(text[1]) == 2 and len(text[2]) == 2:
            return True
        else:
            return False
    else:
        return  False



#防止IP被限制所做的处理
def getRandomWaitTime():

    prob_num = 1
    #平均数大于4，表示一分钟内小于15次
    seed_num = [2,3,4,5,6,7]
    prob_num = random.randint(0, 5)
    time.sleep(seed_num[prob_num])

if __name__ == '__main__':
    trans_for = noLightDateToChDate('0304')
    print(trans_for)
    print(getLastYearMonthDate("2018-12-25"))
    print(getStandarsDate("20181225"))

